package edu.unr.cater.npap.share.domain;

import java.util.Arrays;
import java.util.List;

import javax.persistence.ManyToOne;
import javax.persistence.Query;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class File {

    @NotNull
    @Size(max = 128)
    private String fileName;

    @NotNull
    @Size(max = 128)
    private String filepath;

    @NotNull
    private long filesize;

    @NotNull
    private String contentType;

    @Transient
    private byte[] content;

    @ManyToOne
    private Project idproject;
    
    public static List<File> findFileEntriesWhere(int firstResult, int maxResults, Integer idProject) {
    	List<Project> idprojects = Arrays.asList(Project.findProject(idProject));
    	Query query=entityManager().createQuery("FROM File o WHERE o.idproject in (:idprojects)").setFirstResult(firstResult).setMaxResults(maxResults);
    	query.setParameter("idprojects", idprojects);
        return query.getResultList();
    }
    
    public static List<File> findAllFiles(Project idProject) {
    	List<Project> idprojects = Arrays.asList(idProject);
        Query query=entityManager().createQuery("FROM File o WHERE o.idproject in (:idprojects)");
    	query.setParameter("idprojects", idprojects);
        return query.getResultList();
    }
}
